package org.szgb.console.cxht.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class CxhtLocationFormItemTree extends HashMap<String, List<CxhtLocationFormItemTree.Node>> {

    @Data
    public static class Node {
        private String id;
        private String loop;
        private String startNumber;
        private String endPart;
        private String endNumber;
        private Boolean isRight;
        private String remark;

        public Node(String id, String loop, String startNumber, String endPart, String endNumber) {
            this.id = id;
            this.loop = loop;
            this.startNumber = startNumber;
            this.endPart = endPart;
            this.endNumber = endNumber;
        }
    }

    public CxhtLocationFormItemTree() {
    }

    public CxhtLocationFormItemTree(List<CxhtLocationFormItem> cxhtLocationFormItemList) {
        cxhtLocationFormItemList.forEach(cxhtLocationFormItem -> {
            String key = cxhtLocationFormItem.getCxhtFormItem().getStartPart();
            Node node = new Node(
                    cxhtLocationFormItem.getId(),
                    cxhtLocationFormItem.getCxhtFormItem().getLoop(),
                    cxhtLocationFormItem.getCxhtFormItem().getStartNumber(),
                    cxhtLocationFormItem.getCxhtFormItem().getEndPart(),
                    cxhtLocationFormItem.getCxhtFormItem().getEndNumber()
            );
            this.computeIfAbsent(key, k -> new ArrayList<>()).add(node);
        });
    }

    public List<CxhtLocationFormItem> toCxhtLocationFormItemList() {
        List<CxhtLocationFormItem> cxhtLocationFormItemList = new ArrayList<>();
        this.keySet().forEach(key ->
                this.get(key).forEach(node ->
                        cxhtLocationFormItemList.add(
                                new CxhtLocationFormItem(node.getId(), node.getIsRight(), node.getRemark()))
                )
        );
        return cxhtLocationFormItemList;
    }
}
